﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MusicAcademy
{
    public partial class frmHoliday : Form
    {
        private int recordID = -1;

        public frmHoliday()
        {
            InitializeComponent();
        }

        private bool isValid()
        {
            bool valid = true;
            if (txtDate.IsValidDate() == false)
                valid = Globals.SetError(txtDate);
            else
                Globals.SetValid(txtDate);
            
            return valid;
        }

        private void frmHoliday_Load(object sender, EventArgs e)
        {
            if (UserManager.HasAccess(this.Name, "View") == false)
            {
                MessageBox.Show("شما اجازه ی دیدن این فرم را ندارید !", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.Close();
            }
            this.holidayTableAdapter.Fill(this.musicAcademyDataSet.Holiday);
        }

        private void btnSave_Click_1(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(Properties.Settings.Default.MusicAcademyConnectionString);
            SqlCommand com = new SqlCommand();
            SqlDataReader dreader;
            
            if (isValid() == false)
                return;

            if (recordID == -1)
            {
                if (UserManager.HasAccess(this.Name, "Insert") == false)
                {
                    MessageBox.Show("شما اجازه ی اضافه کردن رکورد را ندارید !", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                com.Connection = con;
                com.CommandType = CommandType.Text;
                com.CommandText = "insert into Holiday(date,reason,academyID, creator, createDate) values(@date,@reason,@academyID, @creator, @createDate); SELECT TOP 1 ID FROM Holiday ORDER BY ID DESC";
                com.Parameters.AddWithValue("@date", txtDate.Text);
                com.Parameters.AddWithValue("@reason", txtReason.Text);
                com.Parameters.AddWithValue("@academyID", Program.academyID);
                com.Parameters.AddWithValue("@creator", Program.userID);
                com.Parameters.AddWithValue("@createDate", Globals.Get10CharDate());
                con.Open();
                dreader = com.ExecuteReader();
                dreader.Read();
                recordID = int.Parse(dreader["ID"].ToString());
                dreader.Close();
                this.holidayTableAdapter.Fill(this.musicAcademyDataSet.Holiday);
                con.Close();
                btnDelete.Enabled = true;

                ClearForm();
                txtDate.Focus();
            }
            else
            {
                if (UserManager.HasAccess(this.Name, "Update") == false)
                {
                    MessageBox.Show("شما اجازه ی ویرایش کردن رکورد را ندارید !", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                com.Connection = con;
                com.CommandType = CommandType.Text;
                com.CommandText = "UPDATE Holiday SET date=@date,reason=@reason, lastEditor=@lastEditor, editDate=@editDate WHERE ID=@ID";
                com.Parameters.AddWithValue("@date", txtDate.Text);
                com.Parameters.AddWithValue("@reason", txtReason.Text);
                com.Parameters.AddWithValue("@ID", recordID);
                com.Parameters.AddWithValue("@lastEditor", Program.userID);
                com.Parameters.AddWithValue("@editDate", Globals.Get10CharDate());
                con.Open();
                com.ExecuteNonQuery();
                this.holidayTableAdapter.Fill(this.musicAcademyDataSet.Holiday);
                con.Close();
            }
            MessageBox.Show("ذخیره شد", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void btnDelete_Click_1(object sender, EventArgs e)
        {
            if (UserManager.HasAccess(this.Name, "Delete") == false)
            {
                MessageBox.Show("شما اجازه ی حذف کردن رکورد را ندارید !", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            if (MessageBox.Show("آیا از حذف این رکورد اطمینان دارید ؟", "حذف", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                SqlConnection con = new SqlConnection(Properties.Settings.Default.MusicAcademyConnectionString);
                SqlCommand com = new SqlCommand();

                com.Connection = con;
                com.CommandType = CommandType.Text;
                com.CommandText = "delete from Holiday where ID=@id";
                com.Parameters.AddWithValue("@id", recordID);
                con.Open();
                com.ExecuteNonQuery();
                this.holidayTableAdapter.Fill(this.musicAcademyDataSet.Holiday);
                con.Close();

                txtDate.Text = "";
                txtReason.Text = "";
                recordID = -1;
                btnDelete.Enabled = false;
                MessageBox.Show("حذف شد", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void grdHoliday_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1)
                return;

            txtDate.Text = grdHoliday.Rows[e.RowIndex].Cells["_Date"].Value.ToString();
            txtReason.Text = grdHoliday.Rows[e.RowIndex].Cells["reason"].Value.ToString();

            btnDelete.Enabled = true;

            recordID = int.Parse(grdHoliday.Rows[e.RowIndex].Cells["_ID"].Value.ToString());

            Globals.SetValid(txtDate);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            ClearForm();
        }

        private void ClearForm()
        {
            txtDate.Text = "";
            txtReason.Text = "";

            btnDelete.Enabled = false;

            recordID = -1;

            Globals.SetValid(txtDate);
        }
    }
}
